﻿using Coldairarrow.Entity.Foot;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Foot
{
    public class Foot_ManageBusiness : BaseBusiness<Foot_Manage>, IFoot_ManageBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public Foot_ManageBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口
        public async Task<List< Foot_Manage>> GetTheDataAsync(string id)
        {
            var aa = GetIQueryable().Where(q => q.id == id).FirstOrDefault().Parent_Id;
            return await GetIQueryable().Where(q => q.id == id).Select(s => new Foot_Manage
            {
                id = s.id,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = s.Deleted,
                Names = s.Names,
                name = GetIQueryable().Where(q=>q.id == aa).FirstOrDefault().Names,
                Parent_Id = s.Parent_Id,
                FontContent = s.FontContent,
                URL = s.URL,
                DATA1 = s.DATA1,
                DATA2 = s.DATA2,
            }).ToListAsync();
        }

        public List<Foot_Manage> All()
        {
            List<Foot_Manage> aa = GetIQueryable().ToList();

            var level1 = aa.Where(a => a.Parent_Id == "0")
                        .Select(a =>
                        {
                            a.children = aa.Where(b => b.Parent_Id == a.id).OrderBy(x => x.Sort).ToList();
                            return a;
                        }).OrderBy(x => x.Sort).ToList();

            foreach (var item in level1)
            {
                if (item.children.Count == 0)
                {

                    item.children = null;
                }
                else
                {
                    foreach (var item2 in item.children)
                    {
                        if (item2.children?.Count == 0)
                        {

                            item2.children = null;
                        }
                    }
                }

            }
            return level1;
        }
        public async Task<List<Foot_Manage>> TwoAll()
        {
            return await GetIQueryable().Where(a => a.Parent_Id == "0").ToListAsync();
        }
        public async Task AddDataAsync(Foot_Manage data)
        {
            Foot_Manage data1 = new Foot_Manage()
            {
                id = GuidHelper.GenerateKey(),
                CreatorId = _operator.UserId,
                CreateTime = data.CreateTime,
                Deleted = data.Deleted,
                Names = data.Names,
                Sort = data.Sort,
                Parent_Id = data.Parent_Id,
                FontContent = data.FontContent,
                URL = data.URL,
                DATA1 = data.DATA1,
                DATA2 = data.DATA2,
            };
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(Foot_Manage data)
        {
            Foot_Manage data1 = new Foot_Manage()
            {
                id = data.id,
                CreatorId = _operator.UserId,
                CreateTime = data.CreateTime,
                Deleted = data.Deleted,
                Names = data.Names,
                Sort = data.Sort,
                Parent_Id = data.Parent_Id,
                FontContent = data.FontContent,
                URL = data.URL,
                DATA1 = data.DATA1,
                DATA2 = data.DATA2,
            };
            await UpdateAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
            foreach (var item in ids)
            {
                var aa = Db.GetIQueryable<Foot_Manage>(true).Where(a => a.Parent_Id == item).ToList();
                await Db.DeleteAsync<Foot_Manage>(aa);
            }
        }

        #endregion

        #region 私有成员

        #endregion
    }
}